﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using CoreInmobiliario.Data;

namespace CoreInmobiliario.Business
{
    [DataObject]
    public class Noticia
    {
        public static Noticia GetInstance()
        {
            return new Noticia();
        }

        private static DBDataContext GetDBDataContext()
        {
            Factories.DBDataContextFactory factory = new Factories.DBDataContextFactory();
            return factory.CreateDataContext() as DBDataContext;
        }

        public Data.Noticia GetById(int noticiaId)
        {
            Data.Noticia noticia = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    noticia = dataContext.Noticias.FirstOrDefault(n => n.NoticiaId.Equals(noticiaId));
                }
                catch (Exception ex)
                {

                }
            }
            return noticia;
        }

        public List<Data.Noticia> GetList()
        {
            List<Data.Noticia> list = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    list = dataContext.Noticias.ToList();

                }
                catch (Exception ex)
                {

                }
            }
            return list;
        }

        public List<Data.spNoticias_selResult> GetNoticias()
        {
            List<Data.spNoticias_selResult> list = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    list = dataContext.spNoticias_sel().ToList();
                }
                catch (Exception ex)
                {

                }
            }
            return list;
        }

        public Boolean UpdateNoticia(Data.Noticia n)
        {
            using (DBDataContext dc = GetDBDataContext())
            {
                try
                {
                    Data.Noticia noticia = dc.Noticias.FirstOrDefault(not => not.NoticiaId.Equals(n.NoticiaId));
                    noticia.Titulo = n.Titulo;
                    noticia.Descripcion = n.Descripcion;

                    dc.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public Boolean DeleteNoticia(int noticiaId)
        {
            using (DBDataContext dc = GetDBDataContext())
            {
                try
                {
                    Data.Noticia noticia = dc.Noticias.FirstOrDefault(n => n.NoticiaId.Equals(noticiaId));
                    dc.Noticias.DeleteOnSubmit(noticia);
                    dc.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public Boolean InsertNoticia(Data.Noticia noticia)
        {
            using (DBDataContext dc = GetDBDataContext())
            {
                try
                {
                    dc.Noticias.InsertOnSubmit(noticia);
                    dc.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
    }
}
